package edu.langnan.xiaoli.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import edu.langnan.xiaoli.constant.XiaoLiConstants;
import edu.langnan.xiaoli.core.domain.MomentDO;
import edu.langnan.xiaoli.core.dto.CommentDTO;
import edu.langnan.xiaoli.core.dto.MomentDTO;
import edu.langnan.xiaoli.mapper.MomentMapper;
import edu.langnan.xiaoli.mapper.PictureMapper;
import edu.langnan.xiaoli.service.CommentService;
import edu.langnan.xiaoli.service.MomentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cql
 */
@Service
public class MomentServiceImpl implements MomentService {

    @Autowired
    private MomentMapper momentMapper;

    @Autowired
    private CommentService commentService;

    @Autowired
    private PictureMapper pictureMapper;

    @Override
    public void createMoment(MomentDTO momentDTO) {
        MomentDO momentDO = new MomentDO();
        BeanUtils.copyProperties(momentDO, momentDTO);

        // 图片
        List<Integer> imgIds = momentDTO.getImgIds();
        if (imgIds != null && imgIds.size() > 0) {
            StringBuilder sb = new StringBuilder();
            for (Integer imgId : imgIds) {
                sb.append(imgId + XiaoLiConstants.SEPARATOR);
            }
            momentDO.setImgs(sb.toString().trim());
        }

        momentMapper.insert(momentDO);
    }

    @Override
    public void deleteMoment(Integer stuId, Integer momentId) {
        // 1.删除文章信息
        QueryWrapper<MomentDO> wrapper = new QueryWrapper();
        wrapper.eq("id", momentId);
        wrapper.eq("stu_id", stuId);
        momentMapper.delete(wrapper);

        // TODO 2.删除图片信息

        // 3.删除评论信息

    }

    @Override
    public List<MomentDTO> momentList(Integer stuId) {
        List<MomentDTO> result = new ArrayList<>();

        // TODO 可优化
        // 1.获取文章基本信息
        QueryWrapper<MomentDO> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        if (stuId != null && stuId != 0) {
            wrapper.eq("stu_id", stuId);
        }
        List<MomentDO> momentDOS = momentMapper.selectList(wrapper);


        for (MomentDO momentDO : momentDOS) {
            MomentDTO m = new MomentDTO();
            BeanUtils.copyProperties(m, momentDO);

            // 2.获取图片
            String[] imgIds = momentDO.getImgs().split(XiaoLiConstants.SEPARATOR);
            if (imgIds != null && imgIds.length > 0) {
                List<Integer> ids = Arrays.stream(imgIds).map(id -> Integer.parseInt(id)).collect(Collectors.toList());
                List<String> imgUrls = pictureMapper.findPictureListByIds(ids);
                m.setImgUrls(imgUrls);
            } else {
                m.setImgUrls(new ArrayList<>());
            }

            // 3.获取评论信息
            List<CommentDTO> commentDTOS = commentService.findCommentList(momentDO.getId(), XiaoLiConstants.MOMENT_TYPE);
            m.setComments(commentDTOS);

            result.add(m);
        }


        return result;
    }
}
